#include "mainwindow.h"
#include "qfilesystemmodel.h"
#include "qfiledialog.h"
#include "qgroupbox.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowClass())
{
    ui->setupUi(this);

    model_fs = new QFileSystemModel(this);
    ui->treeView->setModel(model_fs);
    ui->listView->setModel(model_fs);
    ui->tableView->setModel(model_fs);

    
    //“触发”一下，见说明
    
    model_fs->setRootPath("");//我的电脑

    //model_fs->setRootPath(QDir::currentPath());//这里填啥似乎都没影响
    //ui->treeView->setRootIndex(model_fs->index(QDir::currentPath()));//当前路径，如果没有，显示我的电脑

    //点击跟着变化
    connect(ui->treeView, &QTreeView::clicked, ui->listView, &QListView::setRootIndex);
    connect(ui->treeView, &QTreeView::clicked, ui->tableView, &QTableView::setRootIndex);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_SetRoot_triggered()
{
    QString dir = QFileDialog::getExistingDirectory(this, "选择目录", QDir::currentPath());
    if (dir.isEmpty())
        return;

    ui->treeView->setRootIndex(model_fs->index(dir));
    ui->listView->setRootIndex(model_fs->index(dir));
    ui->tableView->setRootIndex(model_fs->index(dir));
}

void MainWindow::on_radioButton_ShowAll_clicked()
{
    ui->groupBox_Filter->setEnabled(true);
    model_fs->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
}

void MainWindow::on_radioButton_ShowOnlyDir_clicked()
{
    ui->groupBox_Filter->setEnabled(false);
    model_fs->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
}

void MainWindow::on_checkBox_EnableFilters_clicked(bool checked)
{
    ui->comboBox_Filters->setEnabled(checked);
    ui->pushButton_ApplyFilters->setEnabled(checked);
    model_fs->setNameFilterDisables(!checked);
}

void MainWindow::on_pushButton_ApplyFilters_clicked()
{
    QStringList filter = ui->comboBox_Filters->currentText().trimmed().split(";", Qt::SkipEmptyParts);
    model_fs->setNameFilters(filter);
}

void MainWindow::on_treeView_clicked(const QModelIndex& index)
{
    ui->statusBar->showMessage("文件名： " + model_fs->fileName(index) +
        "  文件路径：" + model_fs->filePath(index) +
        "  文件类型：" + model_fs->type(index) +
        "  文件大小：" + QString::number(model_fs->size(index)) + "B");
}





